package cn.qyd.threadsignal;

import org.checkerframework.checker.units.qual.C;

import java.util.concurrent.CountDownLatch;

/**
 * @author 邱运铎
 * @date 2024-03-19 11:48
 */
public class CountDownLatchDemo {
    static class Service implements Runnable {
        private final String name;
        private final int timeStart;
        private final CountDownLatch countDownLatch;

        public Service(String name, int timeStart, CountDownLatch countDownLatch) {
            this.name = name;
            this.timeStart = timeStart;
            this.countDownLatch = countDownLatch;
        }
        @Override
        public void run() {
            try {
                Thread.sleep(timeStart);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name + "准备好了");
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread t1 = new Thread(new Service("service1",1000,countDownLatch));
        Thread t2 = new Thread(new Service("service",2000,countDownLatch));

        t1.start();
        t2.start();

        countDownLatch.await();
        System.out.println("所有服务都准备好了");
    }
}
